using System;
using System.Collections.Generic;
using SimpleJSON;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

namespace Gj
{
    public class AttrHelper : NodeHelper
    {
        public enum Handle
        {
            None,
            RateToSpeed,
            TimeToSpeed,
            ReplaceValue,
            Time,
        }

        public Handle handle = Handle.None;
        [SelectRef(UIKey.ATTR_KEY)] public string attrKey;
        public string customAttrKey;
        public bool pro;
        public string placeholder = "0";
        public string format;
        
        public List<ReplaceValueItem> list;

        private bool _init;
        private string _s;
        private Text _Text;
        private TextMeshProUGUI _TextPro;
        public TextHelper TextHelper;

        private Text Text
        {
            get
            {
                if (_Text == null) _Text = GetComponent<Text>();
                return _Text;
            }
        }

        private TextMeshProUGUI TextPro
        {
            get
            {
                if (_TextPro == null) _TextPro = GetComponent<TextMeshProUGUI>();
                return _TextPro;
            }
        }

        protected override BindDataType bindDataType => BindDataType.Object;


        protected override void BindData(JSONObject obj)
        {
            var s = StringTools.IsEmpty(customAttrKey) ? obj[attrKey].Value : obj[customAttrKey].Value;
            if (s == null) s = "";
            if (_s != null && _s.Equals(s)) return;
            _s = s;

            if (StringTools.IsEmpty(_s))
                if (!StringTools.IsEmpty(placeholder))
                    _s = placeholder;

            switch (handle)
            {
                case Handle.Time:
                    _s = ((Convert.ToSingle(_s) / 1000)).ToString();
                    break;
                case Handle.TimeToSpeed:
                    _s = (60 / (Convert.ToSingle(_s) / 1000)).ToString();
                    break;
                case Handle.ReplaceValue:
                    var v = Convert.ToSingle(_s);
                    _s = list[list.Count - 1].text;
                    for (var i = 0; i < list.Count; i++)
                        if (v <= list[i].value)
                        {
                            _s = list[i].text;
                            break;
                        }

                    break;
            }

            SetText(StringTools.IsEmpty(format) ? _s : string.Format(format, _s));
        }

        private void SetText(string s)
        {
            if (TextHelper != null)
            {
                TextHelper.SetData(s);
            }
            else if (pro)
            {
                TextPro.text = s;
            }
            else
            {
                Text.text = s;
            }
        }

        [Serializable]
        public class ReplaceValueItem
        {
            public int value;
            public string text;
        }
    }
}